package org.example.mallweb.mallservice.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.example.mallcommon.projo.Article;
import org.example.mallcommon.projo.Comment;
import org.example.mallcommon.result.ReturnResult;
import org.example.mallcommon.vo.CommentFatherVio;
import org.example.mallcommon.vo.CommentFatherVo;
import org.example.mallcommon.vo.CommentsSonVo;
import org.example.mallweb.mallservice.mybatiscommon.Comment.CommentMapper;
import org.example.mallweb.mallservice.mybatiscommon.articles.ArticleMapper;
import org.example.mallweb.mallservice.service.CommentsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 韩先楚
 * 评论服务实现类
 */
@Service
public class CommentsServiceImpl implements CommentsService {

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private ArticleMapper articleMapper;

    /**
     * 获取文章的父评论列表
     *
     * @param page
     * @param count
     * @param articleId
     * @return
     */
    @Override
    public ReturnResult<CommentFatherVio> listFatherComments(Integer page, Integer count, Long articleId) {
        Page<CommentFatherVo> pageResult = commentMapper.selectFatherComments(new Page<CommentFatherVo>(page, count), articleId);
        CommentFatherVio commentFatherVio = new CommentFatherVio();
        commentFatherVio.setCommentFatherVoList(pageResult.getRecords());
        commentFatherVio.setCount(pageResult.getTotal());
        return ReturnResult.success(commentFatherVio);
    }

    /**
     * 获取子评论列表
     *
     * @param fatherId
     * @return
     */
    @Override
    public ReturnResult<List<CommentsSonVo>> listChildComments(Long fatherId) {
        List<CommentsSonVo> commentsSonVoList = commentMapper.selectChildComments(fatherId);
        //补充回复人的nickname
        String fatherNickname = commentMapper.getFatherNickname(fatherId);
        Map<Long, String> map = new HashMap<>();
        map.put(fatherId, fatherNickname);
        for (CommentsSonVo commentsSonVo : commentsSonVoList) {
            map.put(commentsSonVo.getId(), commentsSonVo.getNickName());
        }
        for (CommentsSonVo commentsSonVo : commentsSonVoList)
            commentsSonVo.setReplyNickName(map.get(commentsSonVo.getReplyId()));
        return ReturnResult.success(commentsSonVoList);
    }

    //增加父评论
    @Override
    @Transactional
    public ReturnResult<Long> addFatherComment(Comment comment) {
        comment.setCreateTime(new Date());
        comment.setUpdateTime(new Date());
        commentMapper.insert(comment);
        //更新文章评论数
        articleMapper.update(null, new UpdateWrapper<Article>().setSql("comment_count = comment_count + 1").eq("id", comment.getArticleId()));
        return ReturnResult.success(comment.getId());
    }


    //增加子评论
    @Override
    public ReturnResult<Long> addChildComment(Comment comment) {
        comment.setCreateTime(new Date());
        comment.setUpdateTime(new Date());
        commentMapper.insert(comment);
        //更新父评论的回复数
        articleMapper.update(null, new UpdateWrapper<Article>().setSql("comment_count =comment_count + 1").eq("id", comment.getArticleId()));
        return ReturnResult.success(comment.getId());

    }
}
